<template>
    <div>
        <Card shadow>
            <iview-table
                ref="table"
                :baseURL="queryModel.baseURL"
                :menuId="dataModel.menuId"
                :columns="dataModel.columns"
                :order="dataModel.order"
                :pageNumber="pageModel.pageNumber"
                :pageSize="pageModel.pageSize"
                :defaultFilters="queryModel.defaultFilters"
                v-on="{changeTotalSize:changeTotalSize,events:events,changeSelect:changeSelect}">
            </iview-table>
            <br>
            <iview-page 
                :totalSize="pageModel.totalSize"
                :pageNumber="pageModel.pageNumber" 
                :pageSize="pageModel.pageSize"
                :pageSizeOpts="pageModel.pageSizeOpts"
                v-on="{changePageNumber:changePageNumber,changePageSize:changePageSize}">
            </iview-page>
        </Card>
    </div>
</template>

<script>
import iviewTable from '@/view/common/table/Table.vue';
import iviewPage from '@/view/common/page/Page.vue';
export default {
    components: {
        iviewTable,
        iviewPage
    },
    data () {
        return {
            baseURL: '/user/member/level',
            selectRows: [],
            queryModel: {
                // 网关代理路径(必须)+资源路径(可选)
                baseURL: '/user/member/level',
                defaultFilters: [
                ]
            },
            dataModel: {
                menuId: 235,
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '等级名称',
                        key: 'levelName',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.levelName
                            if (params.row.level) {
                                temp += params.row.level
                            }
                            return h('span', temp)
                        }
                    },{
                        title: '折扣率',
                        key: 'discount',
                        sortable: true
                    },{
                        title: '升级金额',
                        key: 'upAmount',
                        sortable: true
                    },{
                        title: '要求充值金额',
                        key: 'storeAmount',
                        sortable: true
                    },{
                        title: '下一等级',
                        key: 'nextLevelName',
                        sortable: true,
                        render: (h, params) => {
                            return h('span', params.row.nextLevelName)
                        }
                    },{
                        title: '是否默认',
                        key: 'isDefault',
                        sortable: true,
                        render: (h, params) => {
                            let temp = params.row.isDefault
                            if (temp === 0) {
                                temp = '否'
                            } else if (temp === 1) {
                                temp = '是'
                            }
                            return h('span', temp)
                        }
                    }
                ],
                order: 'id desc'
            },
            pageModel: {
                totalSize: 0,
                pageNumber: 1,
                pageSize: 10,
                pageSizeOpts: [10, 50, 100, 500]
            }
        };
    },
    methods: {
        events (item) {
            let path = item.path
            if (path === 'add') {
                this.add()
            } else if (path === 'edit') {
                this.edit()
            } else if (path === 'delete') {
                this.delete()
            }
        },
        add () {
            this.$bus.toPath('/level/details')
        },
        edit () {
            if (this.selectRows.length === 1) {
                let row = this.selectRows[0]
                let query = {
                    row: row
                }
                this.$bus.toPath('/level/details', query)
            } else {
                this.$bus.alert('warning', '请选择一条记录')
            }
        },
        delete () {
            if (this.selectRows.length > 0) {
                let ids = []
                for (let row of this.selectRows) {
                    ids.push(row.id)
                }
                if (ids.length === 1) {
                    this.doDelete(ids)
                } else {
                    this.$Modal.confirm({
                        title: '操作提示',
                        content: '<p>确认删除'+ids.length+'条记录?</p>',
                        onOk: () => {
                            this.doDelete(ids)
                        }
                    })
                }
            } else {
                this.$bus.alert('warning', '请至少选择一条记录')
            }
        },
        doDelete (ids) {
            let url = this.baseURL + '/delete'
            this.$ajax.post(url, ids).then((response) => {
                let {code, msg} = response.data
                if (code === 0) {
                    this.$bus.alert('success', msg)
                    this.refresh()
                } else {
                    this.$bus.alert('error', msg)
                }
            });
        },
        refresh () {
            this.$refs.table.refresh()
        },
        changeSelect (rows) {
            this.selectRows = rows
        },
        changeTotalSize (totalSize) {
            this.pageModel.totalSize = totalSize
        },
        changePageNumber (pageNumber) {
            this.pageModel.pageNumber = pageNumber
        },
        changePageSize (pageSize) {
            this.pageModel.pageSize = pageSize
        },
        init () {
            this.refresh()
        }
    },
    mounted () {
        this.init()
    },
    watch: {
        '$route' (to) {
            if (to.path === '/level') {
                this.init()
            }
        }
    }
}
</script>

<style>

</style>
